package com.unionftech.common.common.ui

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.github.chrisbanes.photoview.PhotoView
import com.unionftech.baselibrary.glide.loadImage
import com.unionftech.common.R
import com.unionftech.common.base.BaseActivity

class FullScreenImageActivity : BaseActivity() {
    private val mPhotoView by lazy { findViewById<PhotoView>(R.id.photo_view) }

    companion object {
        private const val PARAMS_IMAGE_URI = "image_uri"
        private const val PARAMS_IMAGE_URL = "image_url"
        private const val PARAMS_IMAGE_RES = "image_res"

        @JvmStatic
        fun startThis(context: Context, uri: Uri) {
            Intent(context, FullScreenImageActivity::class.java).apply {
                putExtra(PARAMS_IMAGE_URI, uri)
                context.startActivity(this)
            }
        }

        @JvmStatic
        fun startThis(context: Context, url: String) {
            Intent(context, FullScreenImageActivity::class.java).apply {
                putExtra(PARAMS_IMAGE_URL, url)
                context.startActivity(this)
            }
        }

        @JvmStatic
        fun startThis(context: Context, imageRes: Int) {
            Intent(context, FullScreenImageActivity::class.java).apply {
                putExtra(PARAMS_IMAGE_RES, imageRes)
                context.startActivity(this)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out) // 启动时淡入
    }

    override fun initLayout() = R.layout.activity_full_screen_image

    override fun statusBar() {
        fullScreen()
    }

    override fun setListener() {
        super.setListener()
        // 点击退出
        mPhotoView.setOnClickListener(this)
    }

    override fun setView() {
        super.setView()
        hideTitleBar()
        when {
            intent.hasExtra(PARAMS_IMAGE_URI) -> {
                val imageUri = intent.getParcelableExtra<Uri>(PARAMS_IMAGE_URI)
                (mPhotoView as ImageView).loadImage(imageUri)
            }

            intent.hasExtra(PARAMS_IMAGE_URL) -> {
                val imageUrl = intent.getStringExtra(PARAMS_IMAGE_URL)
                (mPhotoView as ImageView).loadImage(imageUrl)
            }

            intent.hasExtra(PARAMS_IMAGE_RES) -> {
                val imageRes = intent.getIntExtra(PARAMS_IMAGE_RES, 0)
                (mPhotoView as ImageView).loadImage(imageRes)
            }
        }
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.photo_view -> {
                finish()
            }
        }
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out) // 退出时淡出
    }
}